package org.apache.lucene.analysis.compound.morphology;

import org.apache.lucene.analysis.util.CharArraySet;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;
public class yaN implements sandhi {
	
	public boolean isInDict(CharArraySet dict, String word) {
		char [] warr = word.toCharArray();
		return dict.contains(warr, 0, word.length());
	}
	
	
	public String normalizeITrans(String word) {
		String nw = "";
		for (int i = 0; i < word.length(); i++) {
			if (i > 0 && i < word.length()-1) {
				if (word.charAt(i) == 'A') {
					nw = nw + "aa";
				}
				else if (word.charAt(i) == 'I') {
					nw = nw + "ii";
				}
				else
					nw = nw + word.substring(i, i+1);
			}
			else
				nw = nw + word.substring(i, i+1);
		}
		return nw;
	}
	
	public String splitDevanagari(String word, CharArraySet dict){
		String a = null;
		for(int i = 0; i < word.length() - 3; i++){
			String s3 = word.substring(i, i+3);
			if(s3.equals("्यु")){
				String w1 = word.substring(0, i) + "ि";
				String w2 = "उ" + word.substring(i+3);
				String w3 = word.substring(0, i) + "ी";
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					a = w1 + " " + w2;
					return a;
				}//end if
				else if(isInDict(dict, w3) && isInDict(dict, w2)){
					a = w3 + " " + w2;
					return a;
				}
			}//end if
			else if(word.substring(i, i+3).equals("्ये")){
				String w1 = word.substring(0, i) + "ि";
				String w2 = "ए" + word.substring(i+3);
				String w3 = word.substring(0, i) + "ि";
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					a = w1 + " " + w2;
					return a;
				}//end if
				else if(isInDict(dict, w3) && isInDict(dict, w2)){
					a = w3 + " " + w2;
					return a;
				}
			}//end else if
			else if(word.substring(i, i+2).equals("्व")){
				String w1 = word.substring(0, i) + "ु";
				String w3 = "अ" + word.substring(i+2);
				String w2 = word.substring(0, i) + "ू";
				if(isInDict(dict, w1) && isInDict(dict, w3)){
					a = w1 + " " + w3;
					return a;
				}//end if
				else if(isInDict(dict, w3) && isInDict(dict, w2)){
					a = w2 + " " + w3;
					return a;
				}
			}//end else if
			else if(word.substring(i, i+2).equals("्र")){
				String w1 = word.substring(0, i+1) + "रु";
				String w2 = "अ" + word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if(word.substring(i, i+2).equals("्य")){
				String w1 = word.substring(0, i) + "ि";
				String w2 = "अ" + word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
		}//end for
		return null;
	}//end method

	public String split(char[] wordArrIn, int len, CharArraySet dict){
		String word="";
		for (int i = 0; i < len; i++)
			word += wordArrIn[i];
		word = normalizeITrans(word);
		word = word.toLowerCase();
		String wd = splitDevanagari(word, dict);
		if (wd != null)
			return wd;
		char[] wordArr = word.toCharArray();
		for(int i = 0; i < word.length() - 2; i++){
			if(word.substring(i, i+2).equals("yu")){
				String w1 = word.substring(0, i) + "ii";
				String w2 = word.substring(i+1);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end if
			else if(word.substring(i, i+2).equals("ya")){
				String w1 = word.substring(0, i) + "ii";
				String w2 = word.substring(i+1);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if(word.substring(i, i+2).equals("va")){
				String w1 = word.substring(0, i) + "u";
				String w3 = word.substring(i+1);
				if(isInDict(dict, w1) && isInDict(dict, w3)){
					String a = w1 + " " + w3;
					return a;
				}//end if
			}//end else if
			else if((i+3 <= word.length()) && word.substring(i, i+3).equals("vai")){
				String w1 = word.substring(0, i) + "u";
				String w2 = word.substring(i+1);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if 
			else if((i+3 <= word.length()) && word.substring(i, i+3).equals("vaa")){
				String w1 = word.substring(0, i) + "u";
				String w2 = word.substring(i+1);
				if(isInDict(dict, w1) &&isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if((i+3 <= word.length()) && word.substring(i, i+3).equals("ava")){
				String w1 = word.substring(0, i) + "uu";
				String w2 = word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if(word.substring(i, i+2).equals("vi")){
				String w1 = word.substring(0, i) + "uu";
				String w2 = word.substring(i+1);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if((i+4 <= word.length()) && word.substring(i, i+4).equals("traa")){
				String w1 = word.substring(0, i+2);
				String w2 = word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 +  " " + w2;
					return a;
				}//end if
			}//end else if
			else if((i+4 <= word.length()) && word.substring(i, i+4).equals("trau")){
				String w1 = word.substring(0, i+2);
				String w2 = word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
			else if((i+4 <= word.length()) && word.substring(i, i+4).equals("trii")){
				String w1 = word.substring(0, i+2);
				String w2 = word.substring(i+2);
				if(isInDict(dict, w1) && isInDict(dict, w2)){
					String a = w1 + " " + w2;
					return a;
				}//end if
			}//end else if
		}//end for
		 //return "Word doesn't contain sandhi.";
		return null;
	}//end method
}//end class
